package com.woniuxy.renters.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.renters.dao.RentersInfo;
import com.woniuxy.renters.model.form.AddCarportForm;
import com.woniuxy.renters.model.form.AddrentersForm;
import com.woniuxy.renters.model.form.AddrentersinfoForm;
import com.woniuxy.renters.model.param.AddCarportParam;
import com.woniuxy.renters.model.param.AddrentersinsertstallParam;
import com.woniuxy.renters.model.param.IdCard;
import com.woniuxy.renters.model.param.LoadAllRentersinfoParam;
import com.woniuxy.renters.service.RentersInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@Api(
        tags = {"出租客个人信息相关接口"}
)
@RequestMapping("/rentersInfo")
public class RentersInfoController {
    @Autowired
    private RentersInfoService rentersInfoService;

    @ApiOperation("出租客身份证识别以及新增身份证个人信息")
    @GetMapping({"/rentersVerifyIdCard"})
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "真实姓名"),@ApiImplicitParam(name = "idcard",value = "身份证")})
    private Result rentersVerifyIdCard(String name, String idcard, @RequestHeader String token) {
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        System.out.println(name);
        System.out.println(idcard);
        IdCard idCard1 = this.rentersInfoService.rentersVerifyIdCard(name, idcard, Integer.valueOf(id));
        return Result.getResult(IdCard.class).setMessage("出租客身份证识别成功及添加个人信息成功").setData(idCard1);
    }



    @ApiOperation("添加出租客个人信息")
    @PostMapping({"/insertRentersInfo"})
    private Result insertRentersInfo(@RequestBody AddrentersinfoForm form,@RequestHeader String token) {
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        this.rentersInfoService.insertRentersInfo(form,id);
        return Result.getResult().setMessage("添加出租客个人信息成功");
    }

    @ApiOperation("修改出租客个人信息")
    @GetMapping({"/updateRentersInfo"})
    @ApiImplicitParam(name = "rentersPhone",value = "出租客电话")
    private Result updateRentersInfo(String rentersinfotel, @RequestHeader String token) {
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        rentersInfoService.updateRentersInfo(rentersinfotel,Integer.valueOf(id));
        return Result.getResult(IdCard.class).setMessage("修改出租客个人信息成功");
    }

    @ApiOperation("修改出租客头像")
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile image, @RequestHeader String token) throws IOException {
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        String url = String.valueOf(rentersInfoService.uploadImage(image,id));
        return Result.getResult(String.class).setMessage("修改用户头像成功").setData(url);
    }

    @ApiOperation("分页查询所有出租客信息")
    @GetMapping("/loadAllRentersinfo")
    public Result<PageInfo> loadAllRentersinfo(LoadAllRentersinfoParam laf){
        LoadAllRentersinfoParam param = BeanUtil.toBean(laf, LoadAllRentersinfoParam.class);
        PageInfo<RentersInfo> pageInfo = rentersInfoService.loadAllRentersinfo(param);
        return Result.getResult(PageInfo.class).setMessage("分页查询所有出租客信息成功").setData(pageInfo);
    }

    @ApiOperation("根据姓名查询出租客")
    @GetMapping("/loadAllRentersinfobyName")
    @ApiImplicitParam(name = "rentersinfoName",value = "出租客姓名")
    public Result<RentersInfo> loadAllRentersinfobyName(String rentersinfoName){
        RentersInfo rentersInfo = rentersInfoService.loadAllRentersinfobyName(rentersinfoName);
        return Result.getResult(RentersInfo.class).setMessage("根据姓名查询出租客信息成功").setData(rentersInfo);
    }

    @ApiOperation("根据id查询出租客详细信息")
    @GetMapping("/loadAllRentersinfobyId")
    @ApiImplicitParam(name = "rentersInfoId",value = "出租客id")
    public Result<RentersInfo> loadAllRentersinfobyId(Integer rentersInfoId){
        RentersInfo rentersInfo = rentersInfoService.loadAllRentersinfobyId(rentersInfoId);
        return Result.getResult(RentersInfo.class).setMessage("根据id查询出租客信息成功").setData(rentersInfo);
    }

    @ApiOperation("根据手机号查询出租客")
    @GetMapping("/loadAllRentersinfoByRentersinforPhone")
    @ApiImplicitParam(name = "rentersinfoPhone",value = "出租客电话")
    public Result<RentersInfo> loadAllUserinfobyUserPhone(String rentersinfoPhone){
        RentersInfo rentersInfo = rentersInfoService.loadAllRentersinfoByRentersinforPhone(rentersinfoPhone);
        return Result.getResult(RentersInfo.class).setMessage("根据手机号查询出租客信息成功").setData(rentersInfo);
    }

    @ApiOperation("查询出租客详细信息")
    @GetMapping("/loadAllRentersInfos")
    public Result<RentersInfo> loadAllRentersInfos(@RequestHeader String token){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        RentersInfo rentersInfo= rentersInfoService.loadAllRentersInfos(id);
        return Result.getResult(RentersInfo.class).setMessage("查询业主信息成功").setData(rentersInfo);
    }

    @ApiOperation("查询出租客是否实名认证")
    @GetMapping("/loadAllRentersInfoss")
    public boolean loadAllRentersInfoss(@RequestHeader String token){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        return rentersInfoService.loadAllRentersInfoss(id);
    }

    @ApiOperation("出租客上传车位时验证是否实名认证(服务间调用)")
    @GetMapping("/haveidcard")
    public Result<Boolean> haveidcard(String id){
        return Result.getResult(Boolean.class).setData(rentersInfoService.loadAllRentersInfoss(id)).setCode(200)
                .setMessage("验证是否实名成功");
    }

    @ApiOperation("出租客车位新增")
    @PostMapping("/addCarport")
    public Result addCarport(@RequestBody AddCarportForm acf){
        AddCarportParam param = BeanUtil.toBean(acf, AddCarportParam.class);
        rentersInfoService.addCarport(param);
        return Result.getResult().setMessage("出租客新增车位成功");
    }

}

